<?php 
/*-- Project Introduce --*/

/**
 * 控制器类 Login 
 *
 * @access public
 */
class JJ_Register 
	extends JCAT_Controller 
{

	/**
	 * 构造函数 
	 * 
	 * @access	public
	 * @param	$aParent	JCAT_IController	父控制器 
	 * @return	void
	 */
	public function JJ_Register(JCAT_IController $aParent)
	{
		// 调用父类构造函数
		$this->JCAT_Controller(__CLASS__,$aParent) ;
		
		// 初始化文档、视图、子控制器 
		$this->Initialize() ;
	}

	/**
	 * 初始化文档、视图、子控制器 
	 * 
	 * @access	protected
	 * @return	void
	 */
	protected function Initialize()
	{
		// 消息队列
		$aMessageQueue = new JCAT_MessageQueue() ;
		JCAT_MessageDispatcher::RegisterMessageQueue($aMessageQueue,JJ_Login::MSGTYPE_FAIL) ;

		// 消息输出对象
		$aMessageQueue->AddPrinter(new JCAT_MessageGtkDialogPrinter($aMessageQueue)) ;
		$this->SetMessageQueue($aMessageQueue) ;
	}

	
	/**
	 * 执行入口函数 
	 * 
	 * @access	public
	 * @param	$arrArg=array	array	参数 
	 * @return	bool
	 */
	public function MainRun(array $arrArg=array())
	{$arrArg ;

		$aView = new JJ_RegisterDialog($this) ;
			
		while(1)
		{
			// 显示视图
			$nResponse = $aView->DoDialog() ;
			if( $nResponse===Gtk::RESPONSE_OK )
			{
				//
				$sUsername =  trim($aView->GetUICtrlExchangeData('EntUsername')) ;
				$sPassword =  trim($aView->GetUICtrlExchangeData('EntPassword')) ;
				$sPasswordRepeat =  trim($aView->GetUICtrlExchangeData('EntPasswordRepeat')) ;
				$sEmail =  trim($aView->GetUICtrlExchangeData('EntEmail')) ;
				$sNickname =  trim($aView->GetUICtrlExchangeData('EntNickname')) ;
				
				// 检查用户名
				if( empty($sUsername) )
				{
					JCAT_Gtk::MessageDialog(JCAT_Language::Sentence('没有填写用户名')) ;
					continue ;
				}
				$sUsername = urlencode($sUsername) ;
				
				// 检查密码
				if( empty($sPassword) )
				{
					JCAT_Gtk::MessageDialog(JCAT_Language::Sentence('没有填写密码')) ;
					continue ;
				}
				if($sPassword!==$sPasswordRepeat)
				{
					JCAT_Gtk::MessageDialog(JCAT_Language::Sentence('两次输入的密码不一致')) ;
					continue ;
				}
				
				// 检查 Email
				if( $sEmail and !JCAT_UICtrlEmailVerifier::VerifierEmail($sEmail) )
				{
					JCAT_Gtk::MessageDialog(JCAT_Language::Sentence('填写的电子邮件格式错误')) ;
					continue ;
				}
				
				$this->sNewUsername = $sUsername ;
				$this->sNewPassword = $sPassword ;
				
				// 连接 服务器
				$aSetting = JJ_Setting::GetGlobalInstance() ;
				$sServerHost = $aSetting->GetItem('Jabber','ServerHost') ;
				$sServerPort = $aSetting->GetItem('Jabber','ServerPort') ;
				$nLoginTimeout = $aSetting->GetItem('Jabber','LoginTimeout') ;
				$aAccount = JJ_Account::CreateFromUsername($sUsername) ;
				$aXMPPConn = new Jabber(false) ;
				if( !$aXMPPConn->connect($sServerHost,$sServerPort,$nLoginTimeout) )
				{
					JCAT_Gtk::MessageDialog(JCAT_Language::Sentence('无法连接到服务器')) ;
					return false ;
				}
				
				// 发送 注册请求
				$aXMPPConn->set_handler("registered",$this,"OnRegistered");
				$aXMPPConn->set_handler("regfailure",$this,"OnRegisteredFailure");
				$aXMPPConn->register($aAccount->GetID(),$sPassword,$sEmail,$sNickname) ;
			
				// 等待 操作结果
				$this->bLooping = true ;
				while($this->bLooping)
				{
					usleep(200000);
					$aXMPPConn->loop_execute_once() ;
				}
				
				// 成功返回
				if( $this->bSuccess )
				{
					
					$this->CollectUser($aAccount) ;
					
					return true ;
				}
				
				// 失败重来
				else 
				{
					continue ;
				}
			}
			else
			{
				// 退出
				break ;
			}
		}
		
		return false ;
	}
	
	/**
	 * 搜集 用户账号
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function CollectUser(JJ_Account $aAccount)
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$sCollectUserNotifyUrl = $aSetting->GetItem('/JJ','CollectUserNotifyUrl') ;
		
		if( $aSetting->GetItem('/JJ','CollectUser') and $sCollectUserNotifyUrl )
		{
			$sCollectUserConfirmSentence = $aSetting->GetItem('/JJ','CollectUserConfirmSentence') ;
			$sCollectUserConfirmSentence = JCAT_Language::Sentence($sCollectUserConfirmSentence,$aAccount) ;

			if( $aSetting->GetItem('/JJ','CollectUserConfirm')
					 and !JCAT_Gtk::ConfirmDialog($sCollectUserConfirmSentence) )
			{
				return ;
			}
			
			$sCollectUserNotifyUrl.= urlencode($aAccount->GetJabberAddress()) ;
			file_get_contents($sCollectUserNotifyUrl) ;
		}
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetNewUsername()
	{
		return $this->sNewUsername ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetNewPassword()
	{
		return $this->sNewPassword ;
	}
	

	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnRegistered()
	{
		$this->bLooping = false ;
		$this->bSuccess = true ;
	}
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnRegisteredFailure()
	{
		$aAccount = JJ_Account::CreateFromUsername($this->sNewUsername) ;
		JCAT_Gtk::MessageDialog(JCAT_Language::Sentence('非常抱歉，用户名 %s (%s) 已经被占用，请选择其它为被使用的用户名。',$this->sNewUsername,$aAccount)) ;
		
		$this->bLooping = false ;
		$this->bSuccess = false ;
	}
	
	const MSGTYPE_FAIL = 'RegisterFail' ;
}

?>